import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from models.user import User
from config import Config, DevelopmentConfig
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

# 创建Flask应用
app = Flask(__name__)
app.config.from_object(DevelopmentConfig)

# 初始化数据库
from models import db
db.init_app(app)

with app.app_context():
    users = User.query.all()
    print('数据库中的用户列表:')
    for user in users:
        print(f'ID: {user.id}, Email: {user.email}, 密码哈希: {user.password_hash[:20]}...')
    
    if not users:
        print('数据库中没有用户，创建默认用户...')
        from werkzeug.security import generate_password_hash
        
        # 创建默认管理员用户
        admin_user = User(
            email='admin@example.com',
            password_hash=generate_password_hash('admin123'),
            is_admin=True
        )
        db.session.add(admin_user)
        db.session.commit()
        print('默认管理员用户已创建: admin@example.com / admin123')